package cn.jd.affairs;

import org.apache.rocketmq.client.producer.LocalTransactionState;
import org.apache.rocketmq.client.producer.TransactionListener;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.common.message.MessageExt;

import java.nio.charset.StandardCharsets;

/**
 * 事务监听器
 */
public class MyTranListener implements TransactionListener {




    /**
     * message 消费者方的数据
     * arg 执行本地事务所需要的数据对象
     * @param message
     * @param
     * @return
     */
    @Override
    public LocalTransactionState executeLocalTransaction(Message message, Object arg) {
        try {
            System.out.println("我执行了本地事务"+new String(message.getBody(), StandardCharsets.UTF_8));
            return LocalTransactionState.COMMIT_MESSAGE;
        } catch (Exception e) {
            e.printStackTrace();
            return LocalTransactionState.ROLLBACK_MESSAGE;
        }
    }

    /**
     *
     * @param messageExt
     * @return
     */
    @Override
    public LocalTransactionState checkLocalTransaction(MessageExt messageExt) {
        System.out.println("本地事务提交失败");
        return LocalTransactionState.COMMIT_MESSAGE;
    }
}
